package com.pxxy.lawconsult.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.pxxy.lawconsult.bean.Case;
import com.pxxy.lawconsult.bean.LawerUser;
import com.pxxy.lawconsult.bean.User;
import com.pxxy.lawconsult.constant.DBConstant;
import com.pxxy.lawconsult.utils.DBUtils;




@WebServlet("/GetLawyerServlet")
public class GetLawerUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String REQUST_LAWERTYPE="requstlawertype";
	private static final String REQUEST_LAWERTYPE_REFRESH="refresh";//刷新
	private static final String REQUEST_LAWERTYPE_LOADING="loading";//加载
	public static List<LawerUser> lawerUserList=null;
	public Gson gson=null;
    public GetLawerUserServlet() {
        super();
    }

    //请求get方法，默认返回post请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		System.out.println(111);
		doPost(request, response);
	}

	//请求post方法
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String start=request.getParameter("start");//得到请求的数据，用于读取提交的表单中的值
		//编码方式
		response.setCharacterEncoding("UTF-8");
		//实例化Gson
		gson=new Gson();
		PrintWriter out=response.getWriter();
		//获取数据，定时更新
		int time = 60*60*1000;
		getAllCase(time);
		//获取数据
		List<LawerUser> lawerUser = new ArrayList<>();
		int startLawerUser=Integer.parseInt("1");//需要返回多少条数据
		int lawerCount=lawerUserList.size()-startLawerUser;
		int maxLawer=0;
		if(lawerCount>=5) {
			maxLawer=5;
		}else {
			maxLawer=startLawerUser;
		}
		
		for(int i=0;i<maxLawer;i++) {
			lawerUser.add(lawerUserList.get(startLawerUser++));
		}
		
		String json=gson.toJson(lawerUser);
		
		for(LawerUser mlawerUser:lawerUser) {
			System.out.println(mlawerUser.getContent());
		}
		out.println(json);//输出json数据给客服端
	}

	private void getAllCase(int time) {
		//获取所有的lawer信息
		Timer timer=new Timer(true);
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				lawerUserList=new ArrayList<>();
				try {
					ResultSet rs=DBUtils.query(DBConstant.TB_LAWER_TABLE, null, null);
					ResultSet user=DBUtils.query(DBConstant.TB_USER_TABLE, User.TEL, LawerUser.TEL);
					
					while (rs.next()) {
						while(user.next()) {
							User user1=new User();
						//实例化律师对象
						LawerUser mlawerUser=new LawerUser();
						//设置律师的值
						mlawerUser.setLawerId(rs.getInt(DBConstant.LAWER_TABLE_LAWER_ID));
						mlawerUser.setLawerName(rs.getString(DBConstant.LAWER_TABLE_LAWER_NAME));
						mlawerUser.setLawerAge(rs.getInt(DBConstant.LAWER_TABLE_LAWER_AGE));
						mlawerUser.setLawerSex(rs.getInt(DBConstant.LAWER_TABLE_LAWER_SEX));
						mlawerUser.setAddress(rs.getString(DBConstant.LAWER_TABLE_LAWER_ADDRESS));
						mlawerUser.setIntroduction(rs.getString(DBConstant.LAWER_TABLE_INTRODUCTION));
//						mlawerUser.setTel(rs.getString(DBConstant.LAWER_TABLE_LAWER_TEL));
						mlawerUser.setNumber(rs.getString(DBConstant.LAWER_TABLE_NUMBER));
						mlawerUser.setSpeciality(rs.getString(DBConstant.LAWER_TABLE_SPECIALITY));
						mlawerUser.setOffice(rs.getString(DBConstant.LAWER_TABLE_OFFICE));
						mlawerUser.setEmail(rs.getString(DBConstant.LAWER_TABLE_LAWER_EMAIL));
						lawerUserList.add(mlawerUser);//加入list集合中
						String name=rs.getString(DBConstant.LAWER_TABLE_LAWER_NAME);
						System.out.println(name);
						System.out.println(lawerUserList);
					}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}, 0,time);
	}
	}

